package com.ocm.burnningstar.View;

import android.app.Fragment;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.blankj.utilcode.util.ScreenUtils;
import com.ocm.burnningstar.Adapter.HorUserWaitAdapter;
import com.ocm.burnningstar.Interface;
import com.ocm.burnningstar.MainActivity;
import com.ocm.burnningstar.Model.User;
import com.ocm.burnningstar.R;
import com.shape.sdk.CourseUser;
import com.shape.sdk.FitnessData;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_free)
public class FreeFragment extends Fragment {

    String T = "FreeFragment";
    @ViewInject(R.id.list_user)
    GridView list_user;
    @ViewInject(R.id.backvideo)
    VideoView videoView1;
    boolean isReady = false;
    MainActivity mainActivity;
    HorUserWaitAdapter wait;

    Interface.ReadeyListener readyLs= new Interface.ReadeyListener() {
        @Override
        public void run(List<FitnessData> data) {
            updateData(data);
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivity = (MainActivity)getActivity();
        mainActivity.SetReadyListener(readyLs);
    }

    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        isReady = true;
    }

    public class MyPrepatedListener implements MediaPlayer.OnPreparedListener {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            videoView1.start();
        }
    }

    public class MyCompetionListener implements MediaPlayer.OnCompletionListener {
        final VideoView videoView;
        MyCompetionListener(VideoView videoView) {
            this.videoView = videoView;
        }

        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            videoView.start();
        }
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        Log.d(T,"onConfigurationChanged");
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            double mwidth = layoutParams.height*0.5625;
            layoutParams.width = layoutParams.width>mwidth?(int)mwidth:layoutParams.width;
            videoView1.setLayoutParams(layoutParams);
        }
        super.onConfigurationChanged(newConfig);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(T,"onCreateView");
        return x.view().inject(this, inflater, container);
    }
    @Override
    public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);
        Log.i(T,"onViewCreated");
        int screenWidth = ScreenUtils.getScreenWidth();
        int screenHeight = (int)(ScreenUtils.getScreenHeight()*1.2);
        int viewWidth = (int)(screenWidth*0.7);
        int viewHeight = (int)(screenHeight*0.7);
        int viewMarLeft = (int)(screenWidth*0.15);
        int viewMarTop = (int)(screenHeight*0.15);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(viewWidth,viewHeight);
        params.setMargins(viewMarLeft+30,viewMarTop,0,0);
        list_user.setLayoutParams(params);
        init();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {// 不在最前端界面显示
        } else {// 重新显示到最前端中
        }
    }

    private void init(){
        Log.i(T,"init");
        list_user.setSelector(new ColorDrawable(Color.TRANSPARENT));
        list_user.setNumColumns(2);
        List<User> users = new ArrayList<>();
        wait = new HorUserWaitAdapter(getActivity(),users);
        list_user.setAdapter(wait);


        String video = "android.resource://" + mainActivity.getPackageName() + "/" + R.raw.shapemp4;
        videoView1.setOnPreparedListener(new MyPrepatedListener());
        videoView1.setOnCompletionListener(new MyCompetionListener(videoView1));
        videoView1.setVideoURI(Uri.parse(video));
    }

    void updateData(List<FitnessData> data){
//        if(!isReady)return;
        List<User> users = new ArrayList<>();
        if(data!=null){
        int len = data.size()>=4?4:data.size();
            if(len>0){
                videoView1.stopPlayback();
                videoView1.setVisibility(View.INVISIBLE);
            }
        for(int i =0;i<len;++i){
            CourseUser cu = data.get(i).getUser();
            User user = new User();
//            Bitmap bm =((BitmapDrawable) getResources().getDrawable(R.drawable.test1)).getBitmap();
//            user.setHeader(bm);
            user.setHeaderStr(cu.getAvatar());
            user.setHeartRate(data.get(i).getHeartRate());
            user.setName(cu.getName());
            users.add(user);
        }}
        wait = new HorUserWaitAdapter(getActivity(),users);
        list_user.setAdapter(wait);
    }
}
